package cn.com.blueInfo.bpm.template.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.template.entity.Event;
import cn.com.blueInfo.bpm.template.enums.EventType;
import cn.com.blueInfo.bpm.template.mapper.EventMapper;
import cn.com.blueInfo.bpm.template.repository.EventRepository;
import cn.com.blueInfo.bpm.template.request.EventDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 事件定义表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class EventRepositoryImpl
        extends ServiceImpl<EventMapper, Event>
        implements EventRepository, BaseRepository<Event, EventDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增事件定义
     * @param event 事件定义参数
     */
    @Override
    public Integer createEvent(Event event) {
        return sqlExecuteResult(save(event));
    }

    /**
     * 批量新增事件定义
     * @param eventList 事件定义参数列表
     */
    @Override
    public Integer batchCreateEvent(List<Event> eventList) {
        return sqlExecuteResult(saveBatch(eventList), eventList.size());
    }

    /**
     * 删除事件定义
     * @param eventId 事件定义ID
     */
    @Override
    public Integer deleteEvent(Long eventId) {
        Event event = new Event();
        event.setId(eventId);
        event.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(event));
    }

    /**
     * 批量删除事件定义
     * @param eventIdList 事件定义ID列表
     */
    @Override
    public Integer batchDeleteEvent(List<Long> eventIdList) {
        List<Event> eventList = new ArrayList<>();
        for (Long eventId : eventIdList) {
            Event event = new Event();
            event.setId(eventId);
            event.setDeleted(BaseDeleted.DELETED);
            eventList.add(event);
        }
        return sqlExecuteResult(removeBatchByIds(eventList), eventList.size());
    }

    /**
     * 更新部分事件定义
     * @param event 事件定义参数
     */
    @Override
    public Integer updateEventPartial(Event event) {
        validateUpdateId(event.getId());
        LambdaUpdateWrapper<Event> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Event::getId, event.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部事件定义
     * @param event 事件定义参数
     */
    @Override
    public Integer updateEvent(Event event) {
        validateUpdateId(event.getId());
        return sqlExecuteResult(updateById(event));
    }

    /**
     * 查询一条事件定义
     * @param eventId 事件定义ID
     */
    @Override
    public Event getEventById(Long eventId) {
        LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Event::getId, eventId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询事件定义
     * @param eventQueryDTO 事件定义参数
     */
    @Override
    public Page<Event> getEventPage(EventDTO.QueryDTO eventQueryDTO) {
        Page<Event> eventPage = new Page<>(
                eventQueryDTO.getPage(), eventQueryDTO.getSize());
        LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, eventQueryDTO);

        BaseDeleted deleted = eventQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(eventPage, queryWrapper);
        } else {
            return page(eventPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 事件定义 数量
     * @param eventQueryDTO 事件定义参数
     */
    @Override
    public Long getEventCount(EventDTO.QueryDTO eventQueryDTO) {
        LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, eventQueryDTO);

        BaseDeleted deleted = eventQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 事件定义
     * @param eventQueryDTO 事件定义参数
     */
    @Override
    public List<Event> getEventList(EventDTO.QueryDTO eventQueryDTO) {
        LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, eventQueryDTO);

        BaseDeleted deleted = eventQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<Event> queryWrapper, EventDTO.QueryDTO eventQueryDTO) {

        Long processId = eventQueryDTO.getProcessId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(processId), Event::getProcessId, processId);

        Long nodeId = eventQueryDTO.getNodeId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(nodeId), Event::getNodeId, nodeId);

        String eventCode = eventQueryDTO.getEventCode();
        queryWrapper.eq(StringUtils.isNotBlank(eventCode), Event::getEventCode, eventCode);

        String eventName = eventQueryDTO.getEventName();
        queryWrapper.eq(StringUtils.isNotBlank(eventName), Event::getEventName, eventName);

        EventType eventType = eventQueryDTO.getEventType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(eventType), Event::getEventType, eventType);

        String eventCondition = eventQueryDTO.getEventCondition();
        queryWrapper.eq(StringUtils.isNotBlank(eventCondition), Event::getEventCondition, eventCondition);

        addQueryWrapper(queryWrapper, eventQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<Event, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                Event::getOrderNumber,
                Event::getTenantId,
                Event::getApplicationId,
                Event::getStatus,
                Event::getDeleted,
                Event::getDeleteUserId,
                Event::getDeleteTime,
                Event::getRemark,
                Event::getCreateUserId,
                Event::getCreateTime,
                Event::getUpdateUserId,
                Event::getUpdateTime,
                Event::getExtend1,
                Event::getExtend2,
                Event::getExtend3);
    }

}
